home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / oolregex / regexp.bas < prev    next >
BASIC Source File  |  1995-12-08  |  6KB  |  170 lines

  1. Attribute VB_Name = "Module1"
  2. Public Declare Function RegExpIndx _
  3. Lib "oolregex.dll" Alias "_OolRegExprVB@16" _
  4. (ByVal inputString As String, ByVal pattern As String, _
  5. ByRef subExpIndx() As Integer, ByVal noCase As Long) As Long
  6.  
  7.  
  8. Public Const E_UNEXPECTED = &H8000FFFF
  9. Public Const E_OUTOFMEMORY = &H8007000E
  10. Public Const E_INVALIDARG = &H80070057
  11. Public Const S_OK = 0
  12. Public Const S_FAIL = 1
  13.  
  14. Public Const E_REGEXPNOEXP = &H80006001           ' The Regular Expression string was NULL
  15. Public Const E_REGEXPTOOBIG = &H80006002          ' The Regular Expression was too big
  16. Public Const E_REGEXPTOOMANYPAREN = &H80006003    ' Too many ()'s
  17. Public Const E_REGEXPUNMATCHPAREN = &H80006004    ' Unmatched ()'s
  18. Public Const E_REGEXPSTARPLUSEMPT = &H80006005    ' *+ operand could be empty
  19. Public Const E_REGEXPNESTED = &H80006006          ' nested *?+
  20. Public Const E_REGEXPINVALIDBRKRANGE = &H80006007 ' invalid [] range
  21. Public Const E_REGEXPUNMATCHBRACKET = &H80006008  ' unmatched []
  22. Public Const E_REGEXPOPFOLLOWSNOTHING = &H80006009     ' ?+* follows nothing
  23. Public Const E_REGEXPTRAILINGSLASHS = &H8000600A  ' trailing backslashes
  24.  
  25. Public Function RegExpErrStr(errcode As Long) As String
  26.  Select Case errcode
  27.     Case E_UNEXPECTED
  28.         RegExpErrStr = "Unexpected Error"
  29.     Case E_OUTOFMEMORY
  30.         RegExpErrStr = "Out of Memory"
  31.     Case E_INVALIDARG
  32.         RegExpErrStr = "Invalid Argument"
  33.     Case E_REGEXPTOOBIG
  34.         RegExpErrStr = "The Regular Expression was too big"
  35.     Case E_REGEXPTOOMANYPAREN
  36.         RegExpErrStr = "Too many ()'s in the Regular Expression"
  37.     Case E_REGEXPUNMATCHPAREN
  38.         RegExpErrStr = "Unmatched ()'s in the Regular Expression"
  39.     Case E_REGEXPSTARPLUSEMPT
  40.         RegExpErrStr = "Possiable problem with *+"
  41.     Case E_REGEXPNESTED
  42.         RegExpErrStr = "Nested *?+ in Regular Expression"
  43.     Case E_REGEXPINVALIDBRKRANGE
  44.         RegExpErrStr = "Invalid [] range in Regular Expression"
  45.     Case E_REGEXPUNMATCHBRACKET
  46.         RegExpErrStr = "Unmatched [] in Regular Expression"
  47.     Case E_REGEXPOPFOLLOWSNOTHING
  48.         RegExpErrStr = "?+* follows nothing in Regular Expression"
  49.     Case E_REGEXPTRAILINGSLASHS
  50.         RegExpErrStr = "Trailing backslashes in Regular Expression"
  51.     Case Else
  52.         RegExpErrStr = "Unknown Error"
  53.  End Select
  54. End Function
  55.  
  56. Public Function RegExpErrCode(errcode As Long) As Long
  57.  Select Case errcode
  58.     Case E_UNEXPECTED
  59.         RegExpErrCode = 1
  60.     Case E_OUTOFMEMORY
  61.         RegExpErrCode = 2
  62.     Case E_INVALIDARG
  63.         RegExpErrCode = 3
  64.     Case E_REGEXPTOOBIG
  65.         RegExpErrCode = 4
  66.     Case E_REGEXPTOOMANYPAREN
  67.         RegExpErrCode = 5
  68.     Case E_REGEXPUNMATCHPAREN
  69.         RegExpErrCode = 6
  70.     Case E_REGEXPSTARPLUSEMPT
  71.         RegExpErrCode = 7
  72.     Case E_REGEXPNESTED
  73.         RegExpErrCode = 8
  74.     Case E_REGEXPINVALIDBRKRANGE
  75.         RegExpErrCode = 9
  76.     Case E_REGEXPUNMATCHBRACKET
  77.         RegExpErrCode = 10
  78.     Case E_REGEXPOPFOLLOWSNOTHING
  79.         RegExpErrCode = 11
  80.     Case E_REGEXPTRAILINGSLASHS
  81.         RegExpErrCode = 12
  82.     Case Else
  83.         RegExpErrCode = 13
  84.  End Select
  85. End Function
  86.  
  87. Public Function RegSub(inputString As String, patternString As String, _
  88. substr As String, returnString As String, Optional pos As Variant) As Boolean
  89.     If IsMissing(pos) Then
  90.         pos = 0
  91.     End If
  92.     Dim subPos As Integer
  93.     subPos = CInt(pos)
  94.     Dim indx(2, 1) As Integer
  95.     Dim fromEnd As Integer
  96.     Dim res As Long
  97.     res = RegExpIndx(inputString, patternString, indx(), 1)
  98.     If FAILED(res) Then
  99.         GoTo ErrorHandler
  100.     End If
  101.     If res = S_OK Then
  102.         If (indx(0, i) < 1 Or indx(1, i) < 1) Then
  103.             returnString = substr
  104.         Else
  105.             fromEnd = Len(inputString) - indx(1, subPos) + 1
  106.             returnString = Left(inputString, indx(0, subPos) - 1) & substr & Right(inputString, fromEnd)
  107.         End If
  108.         RegSub = True
  109.     Else
  110.         RegSub = False
  111.     End If
  112.     Exit Function
  113.  
  114. ErrorHandler:
  115.     ' Raise an exception
  116.     Err.Raise vbObjectError + RegExpErrCode(res), "RegExp", RegExpErrStr(res)
  117.     RegSub = False
  118. End Function
  119.  
  120.  
  121. Public Function RegExp(inputString As String, patternString As String, _
  122. ParamArray subExpresions() As Variant) As Boolean
  123.     Dim indx(2, 1) As Integer
  124.     Dim i As Integer, argNum As Integer, indxnum As Integer
  125.     Dim res As Long
  126.     
  127.     res = RegExpIndx(inputString, patternString, indx(), 1)
  128.     If FAILED(res) Then
  129.         GoTo ErrorHandler
  130.     End If
  131.     If res = S_OK Then
  132.         i = 0
  133.         argNum = UBound(subExpresions, 1)
  134.         indxnum = UBound(indx, 2)
  135.         While i < argNum + 1 And i < indxnum + 1
  136.             If (indx(0, i) < 1 Or indx(1, i) < 1) Then
  137.                 subExpresions(i) = ""
  138.             Else
  139.                subExpresions(i) = Mid(inputString, indx(0, i), indx(1, i) - indx(0, i))
  140.             End If
  141.             i = i + 1
  142.         Wend
  143.         RegExp = True
  144.     Else
  145.         RegExp = False
  146.     End If
  147.     Exit Function
  148.  
  149. ErrorHandler:
  150.     ' Raise an exception
  151.     Err.Raise vbObjectError + RegExpErrCode(res), "RegExp", RegExpErrStr(res), App.helpfile, RegExpErrCode(res)
  152.     RegExp = False
  153. End Function
  154.  
  155.  
  156. Public Function FAILED(hresult As Long) As Boolean
  157.     If hresult < 0 Then
  158.         FAILED = True
  159.     Else
  160.         FAILED = False
  161.     End If
  162. End Function
  163. Public Function SUCCEDED(hresult As Long) As Boolean
  164.     If hresult >= 0 Then
  165.         SUCCEDED = True
  166.     Else
  167.         SUCCEDED = False
  168.     End If
  169. End Function
  170.